package com.egao.common.publicly;

import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.world.entity.WorldGameArticle;
import com.egao.common.module.world.service.WorldGameArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 济世杯新闻公开接口
 * @author lsj
 */
@Api(tags = "济世杯公开新闻")
@RestController
@RequestMapping("${api}")
public class PubliclyWorldGameArticleController extends BaseController {
    @Autowired
    private WorldGameArticleService worldGameArticleService;

    @ApiOperation("查询新闻")
    @OperLog(value = "新闻管理", desc = "分页查询新闻")
    @GetMapping("/publicly/world-game/article")
    public PageResult<WorldGameArticle> page(HttpServletRequest request) {
        PageParam<WorldGameArticle> pageParam = new PageParam<>(request);
        return new PageResult<>(worldGameArticleService.listPage(pageParam), pageParam.getTotal());
    }

    @ApiOperation(value = "查看济世杯新闻")
    @GetMapping("/publicly/world-game/article/detail")
    public JsonResult detail(Integer id) {
        WorldGameArticle worldGameArticle = worldGameArticleService.getById(id);
        if (worldGameArticle == null) {
            return JsonResult.error("没有查询到济世杯新闻");
        }

        // 更新点击量
        int hits = worldGameArticle.getHits() != null ? worldGameArticle.getHits() + 1 : 1;
        worldGameArticle.setHits(hits);
        worldGameArticleService.updateById(worldGameArticle);

        return JsonResult.ok().setData(worldGameArticle);
    }
}

